package Test_2019_3_4.servlet;

import Test_2019_3_4.util.JdbcUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import static Test_2019_3_4.util.Time.addDateMinut;

@WebServlet(name = "landServlet", urlPatterns = {"/login.do"})
public class landServlet extends HttpServlet {

    private static int error;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //post解决中文乱码
        request.setCharacterEncoding("utf-8");
        //获取输入信息
        String userName = request.getParameter("username");
        String userPassword = request.getParameter("userPassWord");
        if (userName.equals("绑定邮箱") || userPassword.equals("账号密码") || userName.equals("") || userPassword.equals("")) {
            //跳转到新的界面
            request.setAttribute("MSG", "账号密码不能为空！");
            request.getRequestDispatcher("./login.jsp").forward(request, response);
        } else {
            try {
                if (compare(userName)) {
                    //输出登陆失败
                    request.setAttribute("MSG", "此号已被冻结，12小时后自动解封，方可正常使用。");
                    request.getRequestDispatcher("./login.jsp").forward(request, response);
                } else {
                    JdbcUtil jdbcUtil = new JdbcUtil();
                    int jd = jdbcUtil.isReally(userName, userPassword);
                    HttpSession session = request.getSession();
                    if (jd > 0) {
                        // 把用户数据保存在session域对象中
                        session.setAttribute("loginName", userName);
                        if (userName.equals("admin")) {
                            session.setAttribute("admin", "管理员");
                        }
                        request.setAttribute("MSG", "登陆成功！");
                        //跳转到新的界面
                        request.getRequestDispatcher("./display.jsp").forward(request, response);
                    } else {
                        if (error == 1) {
                            //输出登陆失败
                            request.setAttribute("MSG", "邮箱密码错误或用户不存在！您还有1次机会~");
                            request.getRequestDispatcher("./login.jsp").forward(request, response);
                            error++;
                        } else if (error == 2) {
                            //设置日期格式
                            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            // new Date()为获取当前系统时间
                            jdbcUtil.updateState(addDateMinut(df.format(new Date()), 12), userName);
                            //输出登陆失败
                            request.setAttribute("MSG", "此号已被冻结，12小时后自动解封，方可正常使用。");
                            request.getRequestDispatcher("./login.jsp").forward(request, response);
                        } else {
                            //输出登陆失败
                            request.setAttribute("MSG", "邮箱密码错误或用户不存在！您还有2次机会~");
                            request.getRequestDispatcher("./login.jsp").forward(request, response);
                            error++;
                        }
                    }
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }

    private boolean compare(String userName) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        JdbcUtil jdbcUtil = new JdbcUtil();
        String time = jdbcUtil.selectState(userName);
        if(time == null){
            time = df.format(new Date());
        }
        //将字符串形式的时间转化为Date类型的时间
        Date a = df.parse(df.format(new Date()));
        Date b = df.parse(time);
        //Date类的一个方法，如果a早于b返回true，否则返回false
        return a.before(b);
    }

    /**
     * 分页
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //get解决中文乱码
        //name = new String(name.getBytes("ISO-8859-1"),"utf-8");
        int page = Integer.parseInt(request.getParameter("page"));
        request.setAttribute("page", page);
        request.getRequestDispatcher("./display.jsp").forward(request, response);
    }
}
